<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Logics\HomePageLogic;
use Illuminate\Support\Facades\Storage;

class HomePageController extends BaseController{

    private function getInstance(){
        static $ob=null;
        if(is_null($ob)){
            $ob= new HomePageLogic();
        }
        return $ob;
    }

    //列表
    public function index()
    {
        $cha_id = (int)request('cha_id');
        $cate_id = (int)request('cate_id',1);

        $para=[
            'cha_id'=>$cha_id,
            'cate_id'=>$cate_id
        ];
         $ob= $this->getInstance();
         $arr=$ob->index($para);
         if($arr && is_string($arr)) die(json_error($arr));

        $content= view("admin.homePage.index",$arr) ;

        $data=[
            'content'=>$content,
            'header'=>'首页排版',
        ];
        return  $this->baseList($data);
    }

    /**
     * 删除新品推荐中的排版商品
     */
    public function newGoodsDelete(){
        $id=(int)request('id');
        $ob=$this->getInstance();
        $data=$ob->newGoodsDelete($id);
        $this->result($data);
    }

    //添加新品推荐
    public function addNewGoods(){
        if($_POST){
            $goods_id=(int)request('goods_id');
            $cha_id=(int)request('cha_id');
            $cate_id=(int)request('cate_id');

            $para=[
                'goods_id'=>$goods_id,
                'cha_id'=>$cha_id,
                'cate_id'=>$cate_id
            ];
            $ob=$this->getInstance();
            $data=$ob->addNewGoods($para);
            $this->result($data);
        }
    }

    public function bannerImg(Request $request){
        $id=(int)request('id');
        $cha_id=(int)request('cha_id');
        $cate_id=(int)request('cate_id');

        $para=[
            'id'=>$id,
            'cha_id'=>$cha_id,
            'cate_id'=>$cate_id
        ];

        $ob=$this->getInstance();
        $data=$ob->bannerImg($request,$para);
        $this->result($data);
    }

    //删除banner
    public function bannerDelete(){
        $id=(int)request('id');
        $cha_id=(int)request('cha_id');
        $cate_id=(int)request('cate_id');

        $para=[
            'id'=>$id,
            'cha_id'=>$cha_id,
            'cate_id'=>$cate_id
        ];

        $ob=$this->getInstance();
        $data=$ob->bannerDelete($para);
        $this->result($data);
    }


    //修改banner  跳转地址
    public function ajaxEditBannerUrl(){
        $id=(int)request('id');
        $url= trim(request('url'));
        $para=[
            'id'=>$id,
            'url'=>$url
        ];
        $ob=$this->getInstance();
        $data=$ob->ajaxEditBannerUrl($para);
        $this->result($data);
    }

    //修改排序
    public function ajaxEditSort(){
        $id=(int)request('id');
        $sort= (int)request('sort');
        $para=[
            'id'=>$id,
            'sort'=>$sort
        ];
        $ob=$this->getInstance();
        $data=$ob->ajaxEditSort($para);
        $this->result($data);
    }
















}